package com.pro.service.imple;

import com.pro.common.GuavaCommon;
import com.pro.mapper.JsProductCategroyMapper;
import com.pro.pojo.JsDept;
import com.pro.pojo.JsGoodsCount;
import com.pro.pojo.JsProductCategroy;
import com.pro.service.JsProductCategroyService;
import com.pro.utils.GuavaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Service
public class JsProductCategroyServiceImple implements JsProductCategroyService {
    @Autowired(required = false)
    private JsProductCategroyMapper mapper;
    @Override
    public List<JsProductCategroy> getTree() {
        try {
            Object obj = GuavaUtil.getTenCacheBuild().get(String.valueOf(GuavaCommon.ProductCategory), () -> {
                List<JsProductCategroy> productCategroys = mapper.getTree();
                JsProductCategroy jsProductCategroy = new JsProductCategroy();
                jsProductCategroy.setCategroyId(0);
                jsProductCategroy.setParentId(-1);
                jsProductCategroy.setTitle("选择分类类型");
                productCategroys.add(jsProductCategroy);
                return productCategroys;
            });
            if (obj instanceof List){
                List<JsProductCategroy> list = (List<JsProductCategroy>)obj;
                return list;
            }
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return mapper.getTree();
    }

    @Override
    public Boolean delProductCategroy(Integer categroyId) {
        Boolean aBoolean = mapper.delProductCategroy(categroyId);
        if (aBoolean){
            GuavaUtil.getTenCacheBuild().invalidate(String.valueOf(GuavaCommon.ProductCategory));
        }
        return aBoolean;
    }

    @Override
    public Boolean updateProductCategroy(JsProductCategroy jsProductCategroy) {
        jsProductCategroy.setUpdateDate(new Date());
        Boolean aBoolean = mapper.updateProductCategroy(jsProductCategroy);
        if (aBoolean){
            GuavaUtil.getTenCacheBuild().invalidate(String.valueOf(GuavaCommon.ProductCategory));
        }
        return aBoolean;
    }

    @Override
    public Boolean addProductCategroy(JsProductCategroy jsProductCategroy) {
        jsProductCategroy.setCreateDate(new Date());
        Boolean aBoolean = mapper.addProductCategroy(jsProductCategroy);
        if (aBoolean){
            GuavaUtil.getTenCacheBuild().invalidate(String.valueOf(GuavaCommon.ProductCategory));
        }
        return aBoolean;
    }

    @Override
    public List<JsGoodsCount> collectionCount() {
        return mapper.collectionCount();
    }

    @Override
    public List<JsGoodsCount> hitsSum() {
        return mapper.hitsSum();
    }

    @Override
    public List<JsGoodsCount> commentSum() {
        return mapper.commentSum();
    }
}
